package com.sky.web.example.controller;

import com.sky.common.annotation.Anonymous;
import com.sky.web.example.bean.Student;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Anonymous
@RestController
@RequestMapping("/pojo")
public class PojoController {
    // 处理包含学生信息和地址的POST请求

    /**
     *
     {
     "name": "John Doe",
     "age": 21,
     "address": {
     "street": "123 Main St",
     "city": "Anytown"
     }
     }
     */
    @PostMapping("/student")
    public String handleStudent(@RequestBody Student student) {
        return "Received student: " + student.toString();
    }

    /**
     *
     [
     {
     "name": "John Doe",
     "age": 21,
     "address": {
     "street": "123 Main St",
     "city": "Anytown"
     }
     },
     {
     "name": "Jane Smith",
     "age": 23,
     "address": {
     "street": "456 Elm St",
     "city": "Othertown"
     }
     }
     ]
     */

    @PostMapping("/students")
    public String handleStudents(@RequestBody List<Student> students) {
        for (Student student : students) {
            System.out.println(student);
        }
        return "Successfully received " + students.size() + " students.";
    }
}